package util;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Silvanei Santos
 */
public class UpperField extends PlainDocument {
    
    private int limit = 0;
    
    public UpperField(){
        super();
    }
    
    public UpperField(int limit){
        this.limit = limit;
    }

    @Override
     public void insertString(int offs, String str, AttributeSet a)
         throws BadLocationException {

         if (str == null) {
             return;
         }
         char[] upper = str.toCharArray();
         for (int i = 0; i < upper.length; i++) {
             upper[i] = Character.toUpperCase(upper[i]);
         }
         
         if(limit > 0 && getLength() == limit)
            return;
         
         super.insertString(offs, new String(upper), a);
     }
 }  
